package com.abel.quiz.services.impl;

import com.abel.quiz.dao.UserInfoPoDao;
import com.abel.quiz.dao.BaseDao;
import com.abel.quiz.po.*;
import com.abel.quiz.services.AuthRoleService;
import com.abel.quiz.services.RelUserRoleService;
import com.abel.quiz.services.UserInfoService;
import com.abel.quiz.vo.UserInfoDomain;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author abel.lin
* @date_time 2017-08-04 13:03
* @description
*/
@Service
public class UserInfoServiceImpl
    extends BaseServiceImpl<BaseDao<UserInfoPo,UserInfoPoCriteria>,UserInfoPo,UserInfoPoCriteria>
    implements UserInfoService {

    @Autowired
    private UserInfoPoDao userInfoPoDao;

    @Autowired
    private AuthRoleService roleService;

    @Autowired
    private RelUserRoleService relUserRoleService;

    public UserInfoServiceImpl(){
        super.baseMapper = userInfoPoDao;
    }

    /**
     * @param
     * @description 根据用户名获取用户
     * @author abel.lin
     * @date_time 2017/8/28 16:00
     */
    public UserInfoPo selectByLoginName(String loginName){
        UserInfoPoCriteria query = new UserInfoPoCriteria();
        query.createCriteria().andLoginNameEqualTo(loginName);
        query.setOrderByClause("id limit 1");
        List<UserInfoPo> list = userInfoPoDao.selectByExample(query);
        if(list != null && list.size() > 0){
            return list.get(0);
        }

        return null;
    }

    public UserInfoDomain selectDomainById(String id){
        UserInfoPo po = userInfoPoDao.selectByPrimaryKey(id);
        UserInfoDomain domain = UserInfoDomain.genDomain(po);
        if(domain != null){
            List<AuthRolePo> roleList = roleService.selectByUserId(domain.getId());
            domain.setRoleList(roleList);
        }

        return domain;
    }

    @Transactional
    public int insertDomain(UserInfoDomain domain){
        int result = validUser(domain);

        if(result == 1){
            UserInfoPo user = JSON.parseObject(JSON.toJSONString(domain), UserInfoPo.class);
            result = userInfoPoDao.insertSelective(user);

            if(domain.getRoleList() != null && domain.getRoleList().size() > 0){
                for (AuthRolePo role:domain.getRoleList()){
                    RelUserRolePo ur = new RelUserRolePo();
                    ur.setUserId(user.getId());
                    ur.setRoleId(role.getId());
                    relUserRoleService.insertSelective(ur);
                }
            }
        }

        return result;
    }

    @Transactional
    public int updateDomainByPrimaryKey(UserInfoDomain domain){
        int result = validUser(domain);

        if(result == 1){
            UserInfoPo user = JSON.parseObject(JSON.toJSONString(domain), UserInfoPo.class);
            result = userInfoPoDao.updateByPrimaryKeySelective(user);

            if(domain.getRoleList() != null && domain.getRoleList().size() > 0){
                RelUserRolePoCriteria query = new RelUserRolePoCriteria();
                query.createCriteria().andUserIdEqualTo(user.getId());
                relUserRoleService.deleteByExample(query);
                for (AuthRolePo role:domain.getRoleList()){
                    RelUserRolePo ur = new RelUserRolePo();
                    ur.setUserId(user.getId());
                    ur.setRoleId(role.getId());
                    relUserRoleService.insertSelective(ur);
                }
            }
        }

        return result;
    }

    /**
     * @param
     * @description 验证用户数据
     * @return [1：数据合法，-90：用户对象为空，-91：用户名已存在，-92：手机号码已存在，-93：邮箱已存在]
     * @author abel.lin
     * @date_time 2017/8/28 16:20
     */
    public int validUser(UserInfoDomain domain){
        if(domain != null){
            UserInfoPoCriteria query = new UserInfoPoCriteria();
            UserInfoPoCriteria.Criteria criteria = query.createCriteria();
            boolean userExists = false;
            if(StringUtils.isNotBlank(domain.getId())){
                userExists = true;
            }
            if(StringUtils.isNotBlank(domain.getLoginName())){
                criteria.andLoginNameEqualTo(domain.getLoginName());
                if(userExists){
                    criteria.andIdNotEqualTo(domain.getId());
                }
                if(userInfoPoDao.countByExample(query) > 0){
                    return -91;
                }
            }
            if(StringUtils.isNotBlank(domain.getMobile())){
                query.clear();
                criteria = query.createCriteria();
                criteria.andMobileEqualTo(domain.getMobile());
                if(userExists){
                    criteria.andIdNotEqualTo(domain.getId());
                }
                if(userInfoPoDao.countByExample(query) > 0){
                    return -92;
                }
            }
            if(StringUtils.isNotBlank(domain.getEmail())){
                query.clear();
                criteria = query.createCriteria();
                criteria.andEmailEqualTo(domain.getEmail());
                if(userExists){
                    criteria.andIdNotEqualTo(domain.getId());
                }
                if(userInfoPoDao.countByExample(query) > 0){
                    return -93;
                }
            }
        }else{
            return -90;
        }

        return 1;
    }

}
